<?php

namespace App\Http\Controllers\Home;

use App\Models\Rentalhouse;
use App\Services\Home\AttributeService;
use App\Services\Home\PropertyService;
use App\Services\Home\RegionService;
use App\Services\Home\RentalhouseService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class RentalhouseController extends Controller
{
    protected $properservice;
    protected $attrService;
    protected $service;
    protected $regionService;

    public function __construct(RentalhouseService $rentalhouseService, PropertyService $propertyService, AttributeService $attributeService, RegionService $regionService)
    {
        $this->service = $rentalhouseService;
        $this->properservice = $propertyService;
        $this->attrService = $attributeService;
        $this->regionService = $regionService;
    }

    public function index(Request $request,$id)
    {
        // 市id
//        $id = $request->get('id');
        $areaid = isset($id) ? $id : 0;
        $city_info = $this->regionService->getCityInfo($areaid);
        //属性
        $area = $this->attrService->getAttributeById($areaid);
        // 朝向
        $direction = $this->attrService->getAttributeById(84);
        // 装修
        $renovation = $this->attrService->getAttributeById(47);
        //特色
        $tags = $this->attrService->getAttributeById(78);
        // 房间配置
        $supporting = $this->attrService->getAttributeById(52);
        //搜索词
        $keyword = $request->get('keyword')??'';
        return view(env('DEFAULT_TEMPLATE') . '.rentalhouse', compact('area', 'areaid', 'direction', 'renovation', 'tags', 'supporting', 'city_info','keyword'));
    }

    public function data(Request $request)
    {
        return $this->service->data($request);
    }

    public function show($id)
    {
        $data = $this->service->getDetailById($id);
        switch ($data->pay_type) {
            case 0:
                $data['pay_type'] = '押一付一';
                break;
            case 1:
                $data['pay_type'] = '押一付三';
                break;
            default:
                $data['pay_type'] = '押二付三';
                break;
        }
        $handroomNum = count($data->community->handroom->where('status', 'T'));
        $rentalhouseNum = count($data->community->rentalhouse->where('status', 'T'));
        return view(env('DEFAULT_TEMPLATE') . '.rental_detail', compact('data', 'handroomNum', 'rentalhouseNum'));
    }

}
